package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.mapper.ClazzMapper;
import com.demo.mapper.ClazzTeacherMapper;
 import com.demo.mapper.TeacherMapper;
import com.demo.mapper.StudentMapper;
import com.demo.pojo.entity.Clazz;
import com.demo.pojo.entity.ClazzTeacher;
import com.demo.pojo.entity.Teacher;
import com.demo.pojo.entity.Student;
import com.demo.pojo.dto.ClazzListDTO;
import com.demo.service.ClazzService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 班级服务实现类
 */
@Service
public class ClazzServiceImpl implements ClazzService {
    
    @Autowired
    private ClazzMapper clazzMapper;
    
    @Autowired
    private ClazzTeacherMapper clazzTeacherMapper;
    
    @Autowired
    private TeacherMapper teacherMapper;
    
    @Autowired
    private StudentMapper studentMapper;
    
    @Override
    public IPage<ClazzListDTO> getClazzList(Page<Clazz> page, String clazzName, String grade, String dept) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        
        // 班级名称模糊查询
        if (StringUtils.hasText(clazzName)) {
            queryWrapper.like("clazz_name", clazzName);
        }
        
        // 年级精确查询
        if (StringUtils.hasText(grade)) {
            queryWrapper.eq("grade", grade);
        }
        
        // 院系模糊查询
        if (StringUtils.hasText(dept)) {
            queryWrapper.like("dept", dept);
        }
        
        // 按班级编号升序排列
        queryWrapper.orderByAsc("clazz_no");
        
        // 执行分页查询
        IPage<Clazz> clazzPage = clazzMapper.selectPage(page, queryWrapper);
        
        // 转换为DTO
        IPage<ClazzListDTO> dtoPage = new Page<>();
        BeanUtils.copyProperties(clazzPage, dtoPage);
        
        List<ClazzListDTO> dtoList = clazzPage.getRecords().stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
        
        dtoPage.setRecords(dtoList);
        
        return dtoPage;
    }
    
    @Override
    public Clazz getClazzById(Long id) {
        return clazzMapper.selectById(id);
    }
    
    @Override
    public Clazz getClazzByNo(Long clazzNo) {
        QueryWrapper<Clazz> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("clazz_no", clazzNo);
        return clazzMapper.selectOne(queryWrapper);
    }
    
    @Override
    public boolean addClazz(Clazz clazz) {
        // 检查班级编号是否已存在
        if (getClazzByNo(clazz.getClazzNo()) != null) {
            throw new RuntimeException("班级编号已存在");
        }
        
        return clazzMapper.insert(clazz) > 0;
    }
    
    @Override
    public boolean updateClazz(Clazz clazz) {
        // 检查班级编号是否被其他班级使用
        Clazz existingClazz = getClazzByNo(clazz.getClazzNo());
        if (existingClazz != null && !existingClazz.getClazzId().equals(clazz.getClazzId())) {
            throw new RuntimeException("班级编号已被其他班级使用");
        }
        
        return clazzMapper.updateById(clazz) > 0;
    }
    
    @Override
    public boolean deleteClazz(Long id) {
        return clazzMapper.deleteById(id) > 0;
    }
    
    @Override
    public boolean deleteClazzes(List<Long> ids) {
        return clazzMapper.deleteBatchIds(ids) > 0;
    }
    
    /**
     * 将Clazz实体转换为ClazzListDTO
     */
    private ClazzListDTO convertToDTO(Clazz clazz) {
        ClazzListDTO dto = new ClazzListDTO();
        BeanUtils.copyProperties(clazz, dto);
        
        // 查询班级关联的所有教师
        QueryWrapper<ClazzTeacher> ctQueryWrapper = new QueryWrapper<>();
        ctQueryWrapper.eq("clazz_id", clazz.getClazzId());
        List<ClazzTeacher> clazzTeachers = clazzTeacherMapper.selectList(ctQueryWrapper);
        
        List<ClazzListDTO.TeacherInfo> teacherInfoList = new ArrayList<>();
        for (ClazzTeacher ct : clazzTeachers) {
            Teacher teacher = teacherMapper.selectById(ct.getTeacherId());
            if (teacher != null) {
                ClazzListDTO.TeacherInfo teacherInfo = new ClazzListDTO.TeacherInfo();
                teacherInfo.setTeacherId(teacher.getTeacherId());
                teacherInfo.setTeacherName(teacher.getTeName());
                teacherInfoList.add(teacherInfo);
            }
        }
        dto.setTeachers(teacherInfoList);
        
        return dto;
    }
}

